<?php

/*
 * To change this license header, choose License Headers in Appject Appperties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of apps
 *
 * @author chema_000
 */
class Admin_Controllers_Apps extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $listApp = new Admin_Models_tblApps();
        if (isset($_GET['search'])) {
            $this->view->listAllApp = $listApp->getSearch($_GET['search']);
            $this->view->render("apps/index");
        } else {
            if(isset($_GET['status'])){
            $this->view->listAllApp = $listApp->getSearchStatus();
            $this->view->render("apps/index");  
            }else{
            $this->view->listAllApp = $listApp->getAllApp();
            $this->view->render("apps/index");
            }
        }
    }

    public function detailApp() {
        if (isset($_GET['app_id'])) {
            $app = new Default_Models_tblAppducts();
            $this->view->appByID = $app->getAppByID($_GET['app_id']);
        }
        $this->view->render("apps/detailApp");
    }

    public function imgByAppID() {
        if (isset($_GET['app_id'])) {
            $img = new Admin_Models_tblImages();
            $this->view->listImg = $img->getAllImageByIDApp($_GET['app_id']);
            $this->view->render('apps/imgByAppID');
        }
        $this->view->render('apps/index');
    }

    public function form() {
        if (isset($_GET['app_id'])) {
            $appByID = new Admin_Models_tblApps();
            $app = $appByID->getAppByID($_GET['app_id']);
            $this->view->app = $app;
        }
        $this->view->render('apps/form');
    }

    public function saveData() {
        $retult = FALSE;
        $app = new Admin_Models_tblApps();
        $upImg = new Libs_uploadImg();
        $app->setTitle($_POST['txtTitle']);
        $app->setDescription($_POST['txtDes']);
        if (!empty($_FILES["images"]["name"][0])) {
            if (isset($_POST['txtAppID'])) {
                unlink($app->getAppByID($_POST['txtAppID'])->getImages());
            }
            $url_img = "template/default/images";
            $img = $upImg->addImg($url_img, 'images', $_FILES["images"]["name"]);
            $app->setImages($img[0]);
        } else {
            if (isset($_POST['txtAppID'])) {
                $img = $app->getAppByID($_POST['txtAppID'])->getImages();
                $app->setImages($img);
            } else {
                $app->setImages('No img');
            }
        }
        $app->setPoint($_POST['txtPoint']);
        $app->setCategory_id($_POST['category_id']);
        $app->setStatus($_POST['status']);
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $app->setUpdatetime(date("YmdHis", time()));
        $app->setLink($_POST['txtLink']);
        $app->setRequirement($_POST['txtRequirement']);
        $app->setSize($_POST['txtSize']);
        $app->setVersion($_POST['txtVersion']);
        if(isset($_POST['status'])){
            $app->setStatus($_POST['status']);
        }
        if (isset($_POST['txtAppID'])) {
            $app->setId($_POST['txtAppID']);
            if ($app->updateApp($app)) {
                $retult = TRUE;
            }
        } else if ($app->insertApp($app)) {
            $retult = TRUE;
        }
        if ($retult) {
            echo "<script>alert('Success!')</script>";
        } else {
            echo "<script>alert('Fail!')</script>";
            $url = URL_BASE . '/admin/apps/form';
            if ($_POST['txtAppID']) {
                $url.='?app_id=' . $_POST['txtAppID'];
            }
            $this->redir($url);
        }
        $this->redir(URL_BASE . '/admin/apps');
    }

    public function delete() {
        if (isset($_GET["app_id"])) {
            $app = new Admin_Models_tblApps();
            $img = new Admin_Models_tblImages();
            $url_img = $app->getAppByID($_GET['app_id'])->getImages();
            if ($app->deleteApp($_GET["app_id"])) {
                unlink($url_img);
                echo "<script>alert('Delete Success!')</script>";
            } else {
                echo "<script>alert('Delete Fail!')</script>";
            }
        }
        $url = URL_BASE . '/admin/apps';
//        if (isset($_GET['page'])) {
//            $url.='?page=' . $_GET['page'];
//        } else {
//            $url.='?page=1';
//        }
        $this->redir($url);
    }

    public function delListApp() {
        if (isset($_GET['listId'])) {
            $listId = $_GET['listId'];
            $app = new Admin_Models_tblApps();
            $list_url_del = explode(',', $listId);
            if ($app->delListApp($listId)) {
                foreach ($list_url_del as $key => $url_del) {
                    unlink($url_del);
                }
                echo "<script>alert('Delete Success!')</script>";
            } else {
                echo "<script>alert('Delete Fail!')</script>";
            }
        }
        $url = URL_BASE . '/admin/apps';
        if (isset($_GET['page'])) {
            $url.='?page=' . $_GET['page'];
        } else {
            $url.='?page=1';
        }
        $this->redir($url);
    }

    public function saveImg() {
        if (isset($_GET['action'])) {
            $img = new Admin_Models_tblImages();
            $upImg = new Libs_uploadImg();
            $img->setAppId($_GET['app_id']);
            if (!empty($_FILES["img"]["name"][0])) {
                $url_img = "templates/default/images";
                $arr_img = $upImg->addImg($url_img, 'img', $_FILES['img']['name']);
                if ($_GET['action'] == 'insert') {
                    $numImg = $img->numImgByAppID($_GET['app_id']);

                    foreach ($arr_img as $key => $value) {
                        if ($numImg < 4) {
                            $img->setUrl($value);
                            if ($img->insertImg($img)) {
                                echo "<script>alert('Insert Success!')</script>";
                            } else {
                                echo "<script>alert('Insert Fail!')</script>";
                            }
                        } else {
                            echo "<script>alert('Album image exist max 4 image!')</script>";
                            break;
                        }
                        $numImg++;
                    }
                } else {
                    $img->setUrl($arr_img[0]);
                    $img->setImgId($_GET['img_id']);
                    unlink($img->getImgByID($_GET['img_id'])->getUrl());
                    if ($img->updateImg($img)) {
                        echo "<script>alert('Update Success!')</script>";
                    } else {
                        echo "<script>alert('Update Fail!')</script>";
                    }
                }
            }
        }

        $url = URL_BASE . '/admin/apps/imgByAppID?app_id=' . $_GET['app_id'];
        $this->redir($url);
    }

    public function deleteImg() {
        if (isset($_GET['img_id'])) {
            $img = new Admin_Models_tblImages();
            $url_del = $img->getImgByID($_GET['img_id'])->getUrl();
            if ($img->deleteImg($_GET['img_id'])) {
                unlink($url_del);
                echo "<script>alert('Delete Success!')</script>";
            } else {
                echo "<script>alert('Delete Fail!')</script>";
            }
        }
        $url = URL_BASE . '/admin/apps/imgByAppID?app_id=' . $_GET['app_id'];
        $this->redir($url);
    }

    public function delListImg() {
        print_r('aaa');
        die;
        if (isset($_GET['listId'])) {
            $listId = $_GET['listId'];
            $list_url_del = explode(',', $listId);
            $img = new Admin_Models_tblImages();
            if ($img->delListImg($listId)) {
                foreach ($list_url_del as $key => $url_del) {
                    unlink($url_del);
                }
                echo "<script>alert('Delete Success!')</script>";
            } else {
                echo "<script>alert('Delete Fail!')</script>";
            }
        }
        $url = URL_BASE . '/admin/apps/imgByAppID?app_id=' . $_GET['app_id'];
        $this->redir($url);
    }

}
